package ce.com.cenewbluesdk.entity.k6;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.uitl.ByteUtil;
import ce.com.cenewbluesdk.uitl.CRC16;

/**
 * Created by YaoBingkun on 2017/1/4.
 */

public class K6_GpsArgument {
    private int crc16;		//整个数据包的校验值，不包含其自身。
    private int type;			//GPS参数类型，详见GPS_ARGUMENT_TYPE.
    private byte[] time = new byte[4];		//GPS参数信息的时间，即从网络上下载时的手机时间。
    private int len;		//实际数据的长度.
    private byte[] data;		//GPS参数信息。
    public static final int GPS_ONLINE_TYPE = 0;        //GPS在线数据类型
    public static final int GPS_OFFLINE_TYPE = 1;    //GPS离线数据类型
    private int packIndex; //头包偏远量
    private int itemLength; //当前包数据长度（头包）
    private byte[] sendByte;// 整个数据包


    /**
     *
     * @param path 本地文件路径
     */
    public K6_GpsArgument(String path,int type, long timeMillis){
        File file = new File(path);
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte fileContent[] = new byte[(int)file.length()];
            fileInputStream.read(fileContent);
            init(fileContent,type,timeMillis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public K6_GpsArgument(byte[] data,int type,long timeMillis){
        init(data,type,timeMillis);
    }

    private void init(byte[] data,int type,long timeMillis){
        if (data == null){
            throw new IllegalArgumentException("data must not be null");
        }
        this.data = data;
        Log.e("文件长度","" + data.length);
        this.type = type;
        this.time = ByteUtil.intToByte4((int) (timeMillis / 1000));
        Log.e("UTC",ByteUtil.byte4ToInt(time) + "");
        Log.e("UTC",ByteUtil.byte4ToInt(time) + "");
        Log.e("当前时间",CEBC.byte2hex(time));
        this.len = this.data.length;
        this.crc16 = CRC16.crc16(getSendByte(),2);
        Log.e("CRC校验",crc16 + "");
        sendByte = getSendByte();
        this.packIndex = 0;
        itemLength = sendByte.length>500 ? 500 : sendByte.length;
    }


    public void updateIndex(){
        packIndex += itemLength;
        if (packIndex >= sendByte.length){
            return;
        }
        if (sendByte.length - packIndex < 500){
            itemLength = sendByte.length - packIndex;
        }else {
            itemLength = 500;
        }
    }

    public void resetIndex(){
        packIndex = 0;
        itemLength = sendByte.length>500 ? 500 : sendByte.length;
    }

    public List<byte[]> getGpsArgHeadBytesList(){
        List<byte[]> packs = new ArrayList<>();
        while (packIndex < sendByte.length){
            byte[] pack = getGpsArgHeadBytes();
            if (pack == null)
                break;
            packs.add(pack);
        }
        resetIndex();
        return packs;
    }


    /**
     *
     * @return 获取的头包字节流,当全部发送完成时，返回null
     */
    public byte[] getGpsArgHeadBytes(){
        if (packIndex >= sendByte.length){
            resetIndex();
            return null;
        }
        byte[] pack = new byte[itemLength + 7];
        int index = 0;
        System.arraycopy(ByteUtil.intToByte(type,1),0,pack,index,1); // GPS参数类型
        index += 1;
        System.arraycopy(ByteUtil.int2bytes2(data.length+9),0,pack,index,2);//所有要传输数据的长度
        index += 2;
        System.arraycopy(ByteUtil.int2bytes2(itemLength),0,pack,index,2);//当前包数据的长度
        index += 2;
        System.arraycopy(ByteUtil.int2bytes2(packIndex),0,pack,index,2);//当前传输的位置
        index += 2;
        System.arraycopy(sendByte,packIndex,pack,index, itemLength);// 当前包数据
        if (packIndex == 0){  // 组装的第一个头包包含的crc值
            Log.e("pack crc", ByteUtil.byte2ToInt(Arrays.copyOfRange(pack,7,9)) + " ");
        }
        Log.e("pack offset","" + packIndex);
        Log.e("pack length", "" + itemLength);
        updateIndex();
        return pack;
    }




    public int getCrc16() {
        return crc16;
    }

    public void setCrc16(int crc16) {
        this.crc16 = crc16;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public byte[] getTime() {
        return time;
    }

    public void setTime(byte[] time) {
        this.time = time;
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    /**
     * 转换成字节流
     * @return
     */
    public byte[] getSendByte(){
        byte[] sendByte = new byte[data.length + 9];
        int index = 0;
        System.arraycopy(ByteUtil.int2bytes2(crc16),0,sendByte,index,2);
        index += 2;
        System.arraycopy(ByteUtil.intToByte(type,1),0,sendByte,index,1);
        index += 1;
        System.arraycopy(time,0,sendByte,index,4);
        index += 4;
        System.arraycopy(ByteUtil.int2bytes2(len),0,sendByte,index,2);
        index += 2;
        System.arraycopy(data,0,sendByte,index,data.length);
        return sendByte;
    }







}
